package com.lelefly.base.controller;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lelefly.base.model.Result;
import com.lelefly.base.po.UavInfo;
import com.lelefly.constant.Constant;


@Controller
@RequestMapping("/v108")
public class SaveExectpionController extends BaseController{
	private DateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss:SSS");
//	private static final String tomcat_bin = System.getProperty("user.dir");
//	private static final String tomcat_errors = tomcat_bin.substring(0,tomcat_bin.lastIndexOf("bin")) + "errors";
	
	private static String tomcat_bin ="";
	private static String tomcat_errors = "";
	
	@RequestMapping("/adderror")
	 @ResponseBody
	 public Result saveError(HttpServletRequest request,String error){
			tomcat_bin = System.getProperty("user.dir");
			tomcat_errors = tomcat_bin.substring(0,tomcat_bin.lastIndexOf("bin")) + "errors";
			Result result= new Result<>();
			if (StringUtils.isEmpty(error)) {
				throw new IllegalStateException("参数为空");
			}
			try {
				String version= request.getParameter("version");
				String phone_v= request.getParameter("phone_v");
				File file = new File(tomcat_errors);
				if (!file.exists()) {
					file.mkdirs();
				}
				File file2  = new File(tomcat_errors+"/"+phone_v+"_"+version+"_"+df.format(System.currentTimeMillis())+"error.txt");
				if (file2.exists()) {
					file2.delete();
					file2.createNewFile();
				}
				FileWriter writer = new FileWriter(file2);
	            BufferedWriter bw = new BufferedWriter(writer);
	            bw.write(error.toString());
	            bw.close();
	            writer.close();
	            result.setState(Constant.HTTP_STATUS_OK);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				result.setState(Constant.HTTP_STATUS_FAIL);
				getLogger().info("----->>"+e.getLocalizedMessage());
			}
			return result;
		}
	
	
}
